package cn.xuchunh.campusbbs.viewholder;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.utils.Constants;

/**
 * Created by XuChunH on 2015/10/23.
 */
public class PhotoViewHolder extends RecyclerView.ViewHolder {

    public TextView label;
    public SimpleDraweeView icon;
    public View parentView;

    public PhotoViewHolder(View itemView, View parentView) {
        super(itemView);
        label = (TextView) itemView.findViewById(R.id.item_photo_label);
        icon = (SimpleDraweeView) itemView.findViewById(R.id.item_photo_icon);
        this.parentView = parentView;
    }

    public void bindContent(Uri uri, String title) {
        int size = calSize(parentView, itemView);
        ViewGroup.LayoutParams layoutParams = icon.getLayoutParams();
//        layoutParams.height = size;
        layoutParams.width = size;
        icon.setImageURI(uri);
        label.setText(title);
    }

    private int calSize(View parentView, View childView) {
        int temp = parentView.getWidth() - parentView.getPaddingLeft() - parentView.getPaddingRight();
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) childView.getLayoutParams();
        int totalMargin = (layoutParams.leftMargin + layoutParams.rightMargin) * Constants.PHOTO_COLUMN_COUNT;
        return (temp - totalMargin) / Constants.PHOTO_COLUMN_COUNT;
    }

}
